﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using HotelManagement.Common;
using HotelManagement.BL;

namespace HotelManagement.PL.FunctionControl
{
	public partial class TradeSrchUserControl : BaseUserControl
	{
		#region Constructor
		public TradeSrchUserControl()
		{
			InitializeComponent();
		}
		#endregion

        private const string PURCHASE_CN = "买入";
        private const string SALE_CN = "卖出";
        private const string PURCHASE_EN = "Purchase";
        private const string SALE_EN = "Sell";
        private const string TIME_START = " 00:00:00";
        private const string TIME_END = " 23:59:59";

		#region Event Handler
		private void btnClear_Click(object sender, EventArgs e)
		{
			txtGoodsName.Text = string.Empty;
			cobTradeType.SelectedIndex = -1;
			dtTradeFrom.Text = string.Empty;
			dtTradeTo.Text = string.Empty;
            Trade trade = new Trade();
            gdvTradeList.DataSource = trade.TradeCollection;
		}

		private void btnSearch_Click(object sender, EventArgs e)
		{
            Trade trade = GetSearchCondition();
            TradeBL tradeBL = new TradeBL();
            tradeBL.Search(ref trade);
            DataTable dat = trade.TradeCollection;
            foreach (DataRow dr in dat.Rows) 
            {
                if (dr["TYPE"].Equals(PURCHASE_EN))
                {
                    dr["TYPE"] = PURCHASE_CN;
                }
                else if (dr["TYPE"].Equals(SALE_EN))
                {
                    dr["TYPE"] = SALE_CN;
                }
            }
            gdvTradeList.DataSource = trade.TradeCollection;
		}

		private void TradeSrchUserControl_Load(object sender, EventArgs e)
		{
			InitCombobox();
		}
		#endregion

		#region Private Method
		private void InitCombobox()
		{
            string[] arrTradeType = new string[] { string.Empty, PURCHASE_CN, SALE_CN };
            cobTradeType.Items.AddRange(arrTradeType);
		}

        private Trade GetSearchCondition() 
        {
            Trade trade = new Trade();
            trade.GoodsName = txtGoodsName.Text.Trim();
            if(null != cobTradeType.SelectedItem && cobTradeType.SelectedItem.Equals(PURCHASE_CN))
            {
                trade.Type = PURCHASE_EN;
            }
            else if (null != cobTradeType.SelectedItem && cobTradeType.SelectedItem.Equals(SALE_CN))
            {
                trade.Type = SALE_EN;
            }
            trade.TradeTimeStart = dtTradeFrom.Text + TIME_START;
            trade.TradeTimeEnd = dtTradeTo.Text + TIME_END;
             
            return trade;
        }
		#endregion

	}
}
 